/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.gui;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import langman.gui.loadingoverlay.LoadingOverlay;
import langman.gui.tree.TreeControl;
import langman.util.async.TaskExecutorService;
import langman.util.doping.Inject;
import langman.util.doping.SingletonBean;
import langman.util.guifactory.AbstractFXMLBeanControl;
import langman.util.guifactory.BeanControl;
import langman.util.guifactory.GUIFactory;

@SingletonBean
public class MainFrame extends AbstractFXMLBeanControl {
    private final GUIFactory              guiFactory;
    private final ControlSelectionService controlSelectionService;
    private final ActionKeyEventHandler   actionKeyEventHandler;
    private final TaskExecutorService     taskExecutorService;

    @FXML
    private BorderPane contentBorderPane;

    @FXML
    private BorderPane leftContentPane;

    @FXML
    private BorderPane rightContentPane;
    private LoadingOverlay loadingOverlay;

    @Inject
    public MainFrame(GUIFactory guiFactory, ControlSelectionService controlSelectionService, ActionKeyEventHandler actionKeyEventHandler, TaskExecutorService taskExecutorService) {
        this.guiFactory = guiFactory;
        this.controlSelectionService = controlSelectionService;
        this.actionKeyEventHandler = actionKeyEventHandler;
        this.taskExecutorService = taskExecutorService;
    }

    @Override
    public void initializeAfterInjection() {
        TreeControl control = guiFactory.createControl(TreeControl.class);

        leftContentPane.setCenter(control.getNode());

        controlSelectionService.selectedControlProperty().addListener((observableValue, oldValue, newValue) -> updateContent(newValue));
        updateContent(controlSelectionService.getSelectedNode());

        loadingOverlay = guiFactory.createControl(LoadingOverlay.class);
        contentBorderPane.disableProperty().bind(taskExecutorService.runningProperty());
        taskExecutorService.runningProperty().addListener((observableValue, oldValue, newValue) -> {
            StackPane pane = getNode();
            if (newValue) {
                pane.getChildren().add(loadingOverlay.getNode());
            } else {
                pane.getChildren().remove(loadingOverlay.getNode());
            }
        });

        getNode().addEventHandler(KeyEvent.KEY_PRESSED, actionKeyEventHandler);
    }

    private void updateContent(Class<? extends BeanControl> newValue) {
        Node contentNode = newValue != null ? guiFactory.createControl(newValue).getNode() : null;
        rightContentPane.setCenter(contentNode);
    }
}
